package com.huizhonglm.diap.presenters.wallet;

import com.alibaba.fastjson.JSON;
import com.bruce.common.utils.EmptyUtil;
import com.bruce.common.utils.FileUtil;
import com.huizhonglm.diap.api.API;
import com.huizhonglm.diap.api.CommonParams;
import com.huizhonglm.diap.api.CommonResponse;
import com.huizhonglm.diap.api.HttpCallback;
import com.huizhonglm.diap.api.Status;
import com.huizhonglm.diap.api.model.wallet.AliOrderParams;
import com.huizhonglm.diap.api.model.wallet.AliOrderResponse;
import com.huizhonglm.diap.api.model.wallet.CashConfirmParams;
import com.huizhonglm.diap.api.model.wallet.CheckTPWDParams;
import com.huizhonglm.diap.api.model.wallet.IsSettedResponse;
import com.huizhonglm.diap.api.model.wallet.PaymentParams;
import com.huizhonglm.diap.core.utils.UserUtil;
import com.huizhonglm.diap.ui.wallet.iView.PaymentIView;

/**
 * 描述当前类的作用
 *
 * @author LiXiaoLong
 * @version 1.0
 * @since 2015-07-07 11:19
 */
public class PaymentPresenter {

    private PaymentIView iView;

    public PaymentPresenter(PaymentIView iView) {
        this.iView = iView;
    }

    public void getAliOrderInfo(String orderId){
        AliOrderParams params = new AliOrderParams(API.buildUri(API.ALI_ORDER_BY_PAYMENT));
        params.userId = UserUtil.getUserId();
        params.orderId = orderId;
        API.http().post("getAliOrderInfo", params, new HttpCallback() {
            @Override
            public void onSuccess(String result) {
                if (!EmptyUtil.isEmpty(result)) {
                    AliOrderResponse resp = JSON.parseObject(result, AliOrderResponse.class);
                    iView.getAliOrderSuccess(resp);
                } else {
                    iView.getAliOrderFailure();
                }
            }

            @Override
            public void onFailure(Throwable ex) {
                iView.getAliOrderFailure();
            }
        });
    }

    public void onQuickPayment(PaymentParams params) {
        API.http().post("onQuickPayment", params, new HttpCallback() {
            @Override
            public void onSuccess(String result) {
                if (!EmptyUtil.isEmpty(result)) {
                    CommonResponse resp = JSON.parseObject(result, CommonResponse.class);
                    if (resp != null && Status.SUCCESS.equals(resp.getStatus())){
                        iView.onQuickPaymentSuccess();
                    } else {
                        iView.onQuickPaymentFailure("支付失败，请联系客服");
                    }
                } else {
                    iView.onQuickPaymentFailure("支付失败，请联系客服");
                }
            }

            @Override
            public void onFailure(Throwable ex) {
                iView.onQuickPaymentFailure("支付失败，请联系客服");
            }
        });
    }

    public void checkSetTPWD() {
        API.http().post("checkSetTPWD", new CommonParams(API.CHK_SET_TPWD), new HttpCallback() {
            @Override
            public void onSuccess(String result) {
                if (!EmptyUtil.isEmpty(result)) {
                    IsSettedResponse resp = JSON.parseObject(result, IsSettedResponse.class);
                    if (resp != null){
                        if ("Y".equals(resp.getIsSetted())){
                            iView.onCheckSetTPWD(true);
                        } else {
                            iView.onCheckSetTPWD(false);
                        }
                    } else {
                        iView.onQuickPaymentFailure("支付失败，请联系客服");
                    }
                } else {
                    iView.onQuickPaymentFailure("支付失败，请联系客服");
                }
            }

            @Override
            public void onFailure(Throwable ex) {
                iView.onQuickPaymentFailure("支付失败，请检查网络");
            }
        });
    }

    public void checkSetUserInfo() {
        API.http().post("checkSetUserInfo", new CommonParams(API.CHK_SET_USER_INFO), new HttpCallback() {
            @Override
            public void onSuccess(String result) {
                if (!EmptyUtil.isEmpty(result)) {
                    IsSettedResponse resp = JSON.parseObject(result, IsSettedResponse.class);
                    if (resp != null){
                        if ("Y".equals(resp.getIsSetted())){
                            iView.onCheckSetUserInfo(true);
                        } else {
                            iView.onCheckSetUserInfo(false);
                        }
                    } else {
                        iView.onQuickPaymentFailure("支付失败，请联系客服");
                    }
                } else {
                    iView.onQuickPaymentFailure("支付失败，请联系客服");
                }
            }

            @Override
            public void onFailure(Throwable ex) {
                iView.onQuickPaymentFailure("支付失败，请检查网络");
            }
        });
    }

    public void checkTPWD(String passwd){
        CheckTPWDParams params = new CheckTPWDParams();
        params.userId = UserUtil.getUserId();
        params.password = FileUtil.MD5(passwd);
        API.http().post("checkTPWD", params, new HttpCallback() {
            @Override
            public void onSuccess(String result) {
                if (!EmptyUtil.isEmpty(result)) {
                    CommonResponse resp = JSON.parseObject(result, CommonResponse.class);
                    if (resp != null){
                        if (Status.SUCCESS.equals(resp.getStatus())){
                            iView.onCheckTPWD(true);
                        } else {
                            iView.onCheckTPWD(false);
                        }
                    } else {
                        iView.onQuickPaymentFailure("密码错误，请重新输入");
                    }
                } else {
                    iView.onQuickPaymentFailure("密码错误，请重新输入");
                }
            }

            @Override
            public void onFailure(Throwable ex) {
                iView.onQuickPaymentFailure("支付失败，请检查网络");
            }
        });
    }

    public void cashConfirm(CashConfirmParams params) {
        API.http().post("cashConfirm", params, new HttpCallback() {
            @Override
            public void onSuccess(String result) {
                if (!EmptyUtil.isEmpty(result)) {
                    CommonResponse resp = JSON.parseObject(result, CommonResponse.class);
                    if (resp != null && Status.SUCCESS.equals(resp.getStatus())){
                        iView.onCashConfirmSuccess();
                    } else {
                        iView.onCashConfirmFailure("请商家确认失败，请联系客服");
                    }
                } else {
                    iView.onCashConfirmFailure("请商家确认失败，请联系客服");
                }
            }

            @Override
            public void onFailure(Throwable ex) {
                iView.onCashConfirmFailure("请商家确认失败，请联系客服");
            }
        });
    }
}
